<?php 
	session_start();
	//Vars & Functions
	require_once('./include/setting_oj.inc.php');
require_once('./include/memcache.php');

require_once('./include/common_const.inc.php');
	require_once('./include/contest_functions.inc.php');
	require_once('./include/user_check_functions.php');
	
	class Player {
		var $solved=0;
		var $time=0;
		var $p_wa_num; //题目提交错误次数
		var $p_ac_sec; //每道题目AC的时间
		var $user_id;
		var $nick;
		function __construct() {
			$this->solved=0;
			$this->time=0;
			$this->p_wa_num=array(0);
			$this->p_ac_sec=array(0);
		}
		function Add($pid,$sec,$res) { //题号 时间 是否正确 当res==4时表示正确
            global $OJ_CE_PENALTY;
            if (isset($this->p_ac_sec[$pid])&&$this->p_ac_sec[$pid]>0 || $res==11)
				return;
			if ($res!=4){
                if(isset($OJ_CE_PENALTY)&&!$OJ_CE_PENALTY&&$res==11) return;  // ACM WF punish no ce
                if(isset($this->p_wa_num[$pid])) {
					$this->p_wa_num[$pid]++;
				}else{
					$this->p_wa_num[$pid]=1;
				}
			} else {
				$this->p_ac_sec[$pid]=$sec;
				$this->solved++;
				if(!isset($this->p_wa_num[$pid])) $this->p_wa_num[$pid]=0;
				$this->time+=$sec+$this->p_wa_num[$pid]*1200;
			}
		}
	}

	function s_cmp($A,$B) {
		if ($A->solved!=$B->solved) return $A->solved<$B->solved;
		else return $A->time>$B->time;
	}
	
	//Prepares
	$cid=isset($_GET['cid']) ? intval($_GET['cid']) : 0;
	if ($cid==0) {
		echo "No such contest";
		exit(0);
	}
	
	$highlightID = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";

	if($OJ_MEMCACHE){

		$sql="SELECT `start_time`,`title`,`end_time`,`password`,`private` FROM contest WHERE contest_id = {$cid}";
		$result = mysql_query_cache($sql);
		$contestItem = $result[0];

		if ($contestItem) {
			$start_time=strtotime($contestItem['start_time']);
			$end_time=strtotime($contestItem['end_time']);
			$contest_title=$contestItem['title'];
		} else {
			echo "Content Not Exist";
			exit(0);
		}

		if (!$OJ_LOCKRANK) $OJ_LOCKRANK_PERCENT = 0 ;
		$lock_time=$end_time-($end_time-$start_time)*$OJ_LOCKRANK_PERCENT;

		//echo $lock_time.'-'.date("Y-m-d H:i:s",$lock_time);

		//获取比赛题目的数量
		$sql="SELECT count(1) AS probCnt FROM `contest_problem` WHERE `contest_id`={$cid}";
		$result=mysql_query_cache($sql);
		$problemItem=$result[0];
		$problemCount=$problemItem['probCnt'];


		$sql="SELECT users.user_id,users.nick,solution.result,solution.num,solution.in_date 
		FROM (
			SELECT * FROM solution WHERE solution.contest_id={$cid} AND num>=0 and problem_id>0
		) solution INNER JOIN users ON users.user_id=solution.user_id  and users.defunct='N'
		ORDER BY users.user_id,in_date";
		$playerList = mysql_query_cache($sql);
		$playerCount = count($playerList);
		$user_cnt=0;
		$user_name='';


		$playerArr=array(); //比赛人员信息
		for ($i=0;$i<$playerCount;$i++){
			$onePlayer = $playerList[$i];
			$n_user=$onePlayer['user_id'];
			if (strcmp($user_name,$n_user)){
				$user_cnt++;
				$playerArr[$user_cnt]=new Player();
				$playerArr[$user_cnt]->user_id=$onePlayer['user_id'];
				$playerArr[$user_cnt]->nick=$onePlayer['nick'];
				$user_name=$n_user;
			}

			if(time()<$end_time+3600&&$lock_time<strtotime($onePlayer['in_date']))
				$playerArr[$user_cnt]->Add($onePlayer['num'],strtotime($onePlayer['in_date'])-$start_time,0);
			else
				$playerArr[$user_cnt]->Add($onePlayer['num'],strtotime($onePlayer['in_date'])-$start_time,intval($onePlayer['result']));
		}

		usort($playerArr,"s_cmp");
		$first_blood=array();
		for($i=0;$i<$problemCount;$i++){

			$sql="SELECT user_id FROM solution WHERE contest_id={$cid} AND result=4 AND num={$i} ORDER BY in_date LIMIT 1";
			$result=mysql_query_cache($sql);
			$fbResult = $result[0];
			$fbResultCount=count($fbResult);
			$first_blood[$i] = ($fbResultCount) ? $fbResult['user_id'] : "";
		}
		$except_users=array();
		$tmpStr = 'e'.$cid;
		$sql="SELECT `user_id` FROM `privilege` WHERE `rightstr`={$tmpStr} order by user_id";
		$result=mysql_query_cache($sql);
		foreach($result as $one_user){
			array_push($except_users,$one_user["user_id"]);
		}
	}
	else
	{
	$sql=$pdo->prepare("SELECT `start_time`,`title`,`end_time`,`password`,`private` FROM contest WHERE contest_id = ?");
	$sql->execute(array($cid));
	$contestItem = $sql->fetch(PDO::FETCH_ASSOC);

	if ($contestItem) {
		$start_time=strtotime($contestItem['start_time']);
		$end_time=strtotime($contestItem['end_time']);
		$contest_title=$contestItem['title'];
	} else {
		echo "Content Not Exist";
		exit(0);
	}
	if (!$OJ_LOCKRANK) $OJ_LOCKRANK_PERCENT = 0 ;
	$lock_time=$end_time-($end_time-$start_time)*$OJ_LOCKRANK_PERCENT;

	//echo $lock_time.'-'.date("Y-m-d H:i:s",$lock_time);

	//获取比赛题目的数量
	$sql=$pdo->prepare("SELECT count(1) AS probCnt FROM `contest_problem` WHERE `contest_id`=?");
	$sql->execute(array($cid));
	$problemItem=$sql->fetch(PDO::FETCH_ASSOC);
	$problemCount=$problemItem['probCnt'];


	$sql=$pdo->prepare("SELECT users.user_id,users.nick,solution.result,solution.num,solution.in_date 
		FROM (
			SELECT * FROM solution WHERE solution.contest_id=? AND num>=0 and problem_id>0
		) solution INNER JOIN users ON users.user_id=solution.user_id  and users.defunct='N'
		ORDER BY users.user_id,in_date");
	$sql->execute(array($cid));
	$playerList = $sql->fetchAll(PDO::FETCH_ASSOC);
	$playerCount = count($playerList);
	$user_cnt=0;
	$user_name='';

	$playerArr=array(); //比赛人员信息
	for ($i=0;$i<$playerCount;$i++){
		$onePlayer = $playerList[$i];
		$n_user=$onePlayer['user_id'];
		if (strcmp($user_name,$n_user)){
			$user_cnt++;
			$playerArr[$user_cnt]=new Player();
			$playerArr[$user_cnt]->user_id=$onePlayer['user_id'];
			$playerArr[$user_cnt]->nick=$onePlayer['nick'];
			$user_name=$n_user;
        }

		if(time()<$end_time+3600&&$lock_time<strtotime($onePlayer['in_date']))
            $playerArr[$user_cnt]->Add($onePlayer['num'],strtotime($onePlayer['in_date'])-$start_time,0);
		else
            $playerArr[$user_cnt]->Add($onePlayer['num'],strtotime($onePlayer['in_date'])-$start_time,intval($onePlayer['result']));
	}

	usort($playerArr,"s_cmp");
	$first_blood=array();
	for($i=0;$i<$problemCount;$i++){
		$sql=$pdo->prepare("SELECT user_id FROM solution WHERE contest_id=? AND result=4 AND num=? ORDER BY in_date LIMIT 1");
		$sql->execute(array($cid,$i));
		$fbResult = $sql->fetch(PDO::FETCH_ASSOC);
		$fbResultCount=count($fbResult);
		$first_blood[$i] = ($fbResultCount) ? $fbResult['user_id'] : "";
	}

	$except_users=array();
	$tmpStr = 'e'.$cid;
	$sql=$pdo->prepare("SELECT `user_id` FROM `privilege` WHERE `rightstr`=? order by user_id");
	$sql->execute(array($tmpStr));
	$result=$sql->fetchAll(PDO::FETCH_ASSOC);
	foreach($result as $one_user){
        array_push($except_users,$one_user["user_id"]);
	}
	}
	//Page Includes
	require("./pages/contest_ranklist.php");
?>
